package org.restql.db;

import java.util.List;

public class Utility {

	public static String commas(List<String> list) {
		String compStr = "";
		for (String item : list) {
			compStr += item + ",";
		}
		return compStr.substring(0, compStr.length() - 1);
	}

	public static String ands(List<String> list) {
		String compStr = "";
		for (String item : list) {
			compStr += item + " and ";
		}
		return compStr.substring(0, compStr.length() - 5);
	}

	public static String commasAndQuotes(List<String> list) {
		String compStr = "";
		for (String item : list) {
			compStr += "'" + item + "',";
		}
		return compStr.substring(0, compStr.length() - 1);
	}
	
	public static String nameSql2Js (String name) {
		String[][] map = {
				{"_a", "A"},
				{"_b", "B"},
				{"_c", "C"},
				{"_d", "D"},
				{"_e", "E"},
				{"_f", "F"},
				{"_g", "G"},
				{"_h", "H"},
				{"_i", "I"},
				{"_j", "J"},
				{"_k", "K"},
				{"_l", "L"},
				{"_m", "M"},
				{"_n", "N"},
				{"_o", "O"},
				{"_p", "P"},
				{"_q", "Q"},
				{"_r", "R"},
				{"_s", "S"},
				{"_t", "T"},
				{"_u", "U"},
				{"_v", "V"},
				{"_w", "W"},
				{"_x", "X"},
				{"_y", "Y"},
				{"_z", "Z"}
			};
		name = name.toLowerCase();
		for (String[] pair : map) {
			name = name.replaceAll(pair[0], pair[1]);
		}
		return name;
	}

	public static String nameJs2Sql (String name) {
		String[][] map = {
				{"A", "_A"},
				{"B", "_B"},
				{"C", "_C"},
				{"D", "_D"},
				{"E", "_E"},
				{"F", "_F"},
				{"G", "_G"},
				{"H", "_H"},
				{"I", "_I"},
				{"J", "_J"},
				{"K", "_K"},
				{"L", "_L"},
				{"M", "_M"},
				{"N", "_N"},
				{"O", "_O"},
				{"P", "_P"},
				{"Q", "_Q"},
				{"R", "_R"},
				{"S", "_S"},
				{"T", "_T"},
				{"U", "_U"},
				{"V", "_V"},
				{"W", "_W"},
				{"X", "_X"},
				{"Y", "_Y"},
				{"Z", "_Z"}
			};
		String prefixPart = "";
		String sqlNamePart = name;
		if (name.lastIndexOf(":") > 0) {
			prefixPart = name.substring(0, name.lastIndexOf(":"));
			sqlNamePart = name.substring(name.lastIndexOf(":"));
		} else if (name.equals("callback")) {
			prefixPart = name;
			sqlNamePart = "";
		}
		for (String[] pair : map) {
			sqlNamePart = sqlNamePart.replaceAll(pair[0], pair[1]);
		}
		sqlNamePart = sqlNamePart.toUpperCase();
		name = prefixPart + sqlNamePart;
		return name;
	}
}
